<?php

class UserTrack {
	
	static function get(){
		return sprintf('%s %s (%s)', self::getSystem(), self::getBrowser(), self::getIP());
	}

	static function getSystem() {
		$sys = $_SERVER['HTTP_USER_AGENT'];
		if (stripos($sys, "NT 6.1"))
			$os = "Windows 7";
		elseif (stripos($sys, "NT 6.0"))
			$os = "Windows Vista";
		elseif (stripos($sys, "NT 5.1"))
			$os = "Windows XP";
		elseif (stripos($sys, "NT 5.2"))
			$os = "Windows 2003";
		elseif (stripos($sys, "NT 5"))
			$os = "Windows 2000";
		elseif (stripos($sys, "NT 4.9"))
			$os = "Windows ME";
		elseif (stripos($sys, "NT 4"))
			$os = "Windows NT 4.0";
		elseif (stripos($sys, "98"))
			$os = "Windows 98";
		elseif (stripos($sys, "95"))
			$os = "Windows 95";
		elseif (stripos($sys, "Mac"))
			$os = "Mac";
		elseif (stripos($sys, "Linux")){
			if(preg_match('~ubuntu/(\d+\.\d+)~i', $sys, $matches)){
				$os = "Ubuntu{$matches[1]}";
			}
			else{
				$os = "Linux";
			}
		}
		elseif (stripos($sys, "Unix"))
			$os = "Unix";
		elseif (stripos($sys, "FreeBSD"))
			$os = "FreeBSD";
		elseif (stripos($sys, "SunOS"))
			$os = "SunOS";
		elseif (stripos($sys, "BeOS"))
			$os = "BeOS";
		elseif (stripos($sys, "OS/2"))
			$os = "OS/2";
		elseif (stripos($sys, "PC"))
			$os = "Macintosh";
		elseif (stripos($sys, "AIX"))
			$os = "AIX";
		else
			$os = "未知操作系统";

		return $os;
	}

	static function getBrowser() {
		$sys = $_SERVER['HTTP_USER_AGENT'];
		if (stripos($sys, "NetCaptor") > 0)
			$exp = "NetCaptor";
		elseif (stripos($sys, "Firefox/") > 0) {
			preg_match("/Firefox\/([^;)]+)+/i", $sys, $b);
			$exp = "Firefox " . $b[1];
		} elseif (stripos($sys, "MAXTHON") > 0) {
			preg_match("/MAXTHON\s+([^;)]+)+/i", $sys, $b);
			preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
			$exp = $b[0] . " (IE" . $ie[1] . ")";
		} elseif (stripos($sys, "MSIE") > 0) {
			preg_match("/MSIE\s+([^;)]+)+/i", $sys, $ie);
			$exp = "IE" . $ie[1];
		} elseif (stripos($sys, "Netscape") > 0)
			$exp = "Netscape";
		elseif (stripos($sys, "Opera") > 0)
			$exp = "Opera";
		elseif (stripos($sys, "Chromium")>0)
			$exp = "Chromium";
                elseif (stripos($sys, "Chrome")>0)
			$exp = "Chrome";
                elseif (stripos($sys, "Safari")>0)
			$exp = "Safari";
		else
			$exp = "未知浏览器";

		return $exp;
	}

	static function getIP() {
		static $realip;
		if (isset($_SERVER)) {
			if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
				$realip = $_SERVER["HTTP_X_FORWARDED_FOR"];
			} else if (isset($_SERVER["HTTP_CLIENT_IP"])) {
				$realip = $_SERVER["HTTP_CLIENT_IP"];
			} else {
				$realip = $_SERVER["REMOTE_ADDR"];
			}
		} else {
			if (getenv("HTTP_X_FORWARDED_FOR")) {
				$realip = getenv("HTTP_X_FORWARDED_FOR");
			} else if (getenv("HTTP_CLIENT_IP")) {
				$realip = getenv("HTTP_CLIENT_IP");
			} else {
				$realip = getenv("REMOTE_ADDR");
			}
		}
		return $realip;
	}

}
?>
